package cn.modoumama.common.tools;

/**
 * 路径地址操作工具类<br>
 *
 * 创建人：邓强   <br>
 * 创建时间：2018/5/3/003 17:19    <br>
 * @version   V1.0
 */
public class PathUtils {
	/** 项目存放启动地址*/
	public static String ROOT_PATH;
	/** 项目地址*/
	public static String PROJECT_PATH;
	/** 项目地址*/
	public static String CLASS_PATH;

	//空字符串
	public static final String EMPTY = "";
	//点
	public static final String POINT = ".";

	static{
		String path = PathUtils.class.getClassLoader().getResource("").getPath();
		//操作系统
		String os = System.getProperty("os.name").toLowerCase();
		if(os.indexOf("windows")>=0){
			path = path.replaceAll("^/", "");
		}
		CLASS_PATH = path;
        path = path.replace("%20"," ");
        path = path.replaceAll("/classes/", "");
        path = path.replaceAll("/test-classes/", "");
        path = path.replaceAll("/WEB-INF", "");
        PROJECT_PATH = getPathNotEnd(path);
        path = path.replaceAll("([^/]+)$", "");
        ROOT_PATH = getPathNotEnd(path);
	}

	/**
     * 获取文件后缀
	 * @param fileName
     * @return
     */
	public static String getFileSuffix(String fileName) {
		String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
		if(StringUtils.isNotBlank(fileSuffix) && fileSuffix.length()>1){
			fileSuffix = fileSuffix.substring(1);
		}else{
			fileSuffix = null;
		}

		return fileSuffix;
	}


	/**
	 * 返回开头带‘/’的路径
	 * @param path
	 * @return
	 */
	public static String getPathStart(String path){
		if(!path.startsWith("/")){
			path = "/"+path;
		}
		return path;
	}

	/**
	 * 返回开头不带‘/’的路径
	 * @param path
	 * @return
	 */
	public static String getPathNotStart(String path){
		path = path.replaceAll("^\\/", "");

		return path;
	}

	/**
	 * 返回结尾带‘/’的路径
	 * @param path
	 * @return
	 */
	public static String getPathEnd(String path){
		if(!path.endsWith("/")){
			path = path+"/";
		}
		return path;
	}

	/**
	 * 返回结尾不带‘/’的路径
	 * @param path
	 * @return
	 */
	public static String getPathNotEnd(String path){
		path = path.replaceAll("\\/$", "");
		return path;
	}

	/**
	 * 通过获取文件后缀名
	 *
	 * @param path 文件路径
	 * @return 获取后缀命名
	 */
	public static String getPostfix(String path) {
		//传入空串，null，空白符
		if (path != null) {
			path = path.trim();
			//路径中含有".",则匹配后缀名
			if (path.contains(POINT)) {
				return path.substring(path.lastIndexOf(POINT) + 1, path.length());
			}
		}

		return EMPTY;
	}
}
